/**
 * 全局模块配置文件
 * 作为统一入口，导出所有辅助模块和npm包
 */

// 导入辅助模块
const pageHelper = require('../helpers/page_helper.js');
const cloudHelper = require('../helpers/cloud_helper.js');
const timeHelper = require('../helpers/time_helper.js');
const fileHelper = require('../helpers/file_helper.js');
const routerHelper = require('../helpers/router_helper.js');
const dataHelper = require('../helpers/data_helper.js');

// 尝试导入验证模块，支持多种可能的路径
let validate;
try {
  // 路径1: 尝试validate_helper.js
  validate = require('../helpers/validate_helper.js');
  console.log('[module_config.js] 成功加载validate_helper.js');
} catch (e) {
  try {
    // 路径2: 尝试validate.js
    validate = require('../helpers/validate.js');
    console.log('[module_config.js] 成功加载validate.js');
  } catch (err) {
    console.error('[module_config.js] 加载验证模块失败，提供备用实现', e);
    // 提供备用实现
    validate = {
      checkRequired: (val, desc = '') => val ? '' : `${desc}不能为空`,
      isNumber: (val) => !isNaN(Number(val)),
      isEmpty: (val) => val === undefined || val === null || val === '',
      checkMobile: (mobile) => /^1[3-9]\d{9}$/.test(mobile) ? '' : '请输入正确的手机号码',
      checkEmail: (email) => /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(email) ? '' : '请输入正确的邮箱'
    };
  }
}

// 导入npm包配置
const npmConfig = require('./npm_config.js');

// 导入全局Store配置 (优先)
let resourcesStore, backupResourcesStore, backupDefaultStore;

// 公共默认常量
const DEFAULT_CACHE_GROUP = {
  PROJECT: 'project',
  TASK: 'task',
  USER: 'user',
  DOCUMENT: 'document',
  RESOURCE: 'resource',
  MEETING: 'meeting',
  CONFIG: 'config',
  CACHE_KEY: 'DEFAULT_CACHE_KEY',
  CACHE_TIME: 30 * 60 * 1000, // 默认缓存30分钟 
};

// 直接定义备用存储对象，而不是导入
backupResourcesStore = {
  // 必须包含CACHE_GROUP属性，防止"Cannot read property 'CACHE_GROUP' of undefined"错误
  CACHE_GROUP: DEFAULT_CACHE_GROUP,
  
  // 基本状态
  loading: false,
  error: null,
  resources: {},
  cacheMap: new Map(),
  
  // 基本方法
  setLoading(loading) { this.loading = loading; },
  setError(error) { this.error = error; },
  
  // 缓存资源
  cacheResource(group, key, data) {
    console.warn('[module_config.js] 使用备用resourcesStore.cacheResource');
    if (!this.resources[group]) {
      this.resources[group] = {};
    }
    this.resources[group][key] = data;
    return data;
  },
  
  // 获取缓存资源
  getResource(group, key) {
    console.warn('[module_config.js] 使用备用resourcesStore.getResource');
    if (!this.resources[group] || this.resources[group][key] === undefined) {
      return null;
    }
    return this.resources[group][key];
  },
  
  // 检查缓存是否存在
  hasResource(group, key) {
    return this.resources[group] && this.resources[group][key] !== undefined;
  },
  
  // 删除缓存
  removeResource(group, key) {
    console.warn('[module_config.js] 使用备用resourcesStore.removeResource');
    if (this.resources[group] && this.resources[group][key] !== undefined) {
      delete this.resources[group][key];
      return true;
    }
    return false;
  },
  
  // 清除所有缓存
  clearAll() {
    console.warn('[module_config.js] 使用备用resourcesStore.clearAll');
    this.resources = {};
  }
};

// 备用默认存储对象，防止未定义错误
backupDefaultStore = {
  CACHE_GROUP: DEFAULT_CACHE_GROUP,
  loading: false,
  error: null,
  reset() { console.warn('[module_config.js] 使用备用defaultStore.reset'); },
  initialize() {
    console.warn('[module_config.js] 使用备用defaultStore.initialize');
    return this;
  }
};

// 尝试导入store_config，并在失败时使用备用实现
try {
  const storeConfig = require('../store/store_config.js');
  resourcesStore = storeConfig.resourcesStore;
  
  if (!resourcesStore) {
    console.error('[module_config.js] resourcesStore在store_config中为空，使用备用实现');
    resourcesStore = backupResourcesStore;
  } else if (!resourcesStore.CACHE_GROUP) {
    console.error('[module_config.js] resourcesStore.CACHE_GROUP未定义，添加默认值');
    resourcesStore.CACHE_GROUP = DEFAULT_CACHE_GROUP;
  }
  
  console.log('[module_config.js] 成功导入store_config和resourcesStore');
} catch (e) {
  console.error('[module_config.js] 导入store_config失败，创建备用实现', e);
  resourcesStore = backupResourcesStore;
}

// 尝试导入ProjectBiz
let ProjectBiz;
try {
  // 修正路径，使用projects/oa/helpers/project_biz.js
  ProjectBiz = require('../projects/oa/helpers/project_biz.js');
  console.log('[module_config.js] 成功导入ProjectBiz模块');
} catch (e) {
  console.warn('[module_config.js] 导入ProjectBiz失败，使用模拟实现');
  ProjectBiz = {
    initPage: function(that) {
      console.log('ProjectBiz.initPage被调用', that);
      // 基本初始化
      if (that && that.setData) {
        that.setData({
          isInit: true
        });
      }
    }
  };
}

// 导入项目配置
const projectsConfig = require('./projects_config.js');
const projects = require('./projects.js');

// 导出辅助模块
exports.Helpers = {
  pageHelper,
  cloudHelper,
  timeHelper,
  fileHelper,
  routerHelper,
  validate,
  dataHelper,
  // 直接在Helpers中导出resourcesStore
  resourcesStore
};

// 导出npm包
exports.NpmPackages = {
  mobx: npmConfig.mobx,
  createStoreBindings: npmConfig.createStoreBindings,
  storeBindingsBehavior: npmConfig.storeBindingsBehavior,
  dayjs: npmConfig.dayjs
};

// 导出备用Store
exports.BackupStores = {
  resourcesStore: backupResourcesStore,
  defaultStore: backupDefaultStore
};

// 导出实际使用的Store
exports.Stores = {
  resourcesStore: resourcesStore
};

// 单独导出resourcesStore以便直接访问
exports.resourcesStore = resourcesStore;

/**
 * 全局模块统一配置入口
 * 根据模块引用规范，使用统一配置入口点模式
 */

// 导出模型配置
const ModelsConfig = require('./models_config.js');
exports.ModelsConfig = ModelsConfig;

// 导出基础业务逻辑
const BaseBiz = require('./base_biz_config.js');
exports.BaseBiz = BaseBiz;

// 导出项目设置配置
const projectSetting = require('./projects_config.js');
exports.projectSetting = projectSetting;

// 导出公共业务配置
const PublicBiz = require('./public_biz_config.js');
exports.PublicBiz = PublicBiz;

// 导出路由配置
const ROUTES = require('./routes_config.js');
exports.ROUTES = ROUTES;

// 从module_registry整合的项目业务模块
exports.ProjectBiz = ProjectBiz;
exports.projects = projects;
exports.projectsConfig = projectsConfig;
exports.setting = projects; // 项目setting配置 